package com.xiaouyudeguang.common.config;

import com.xiaouyudeguang.common.annotations.ApiSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

import java.util.List;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired(required = false)
    private List<AbstractWebSocketHandler> webSocketHandlers;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        webSocketHandlers.forEach(it -> {
            ApiSocket apiSocket = it.getClass().getAnnotation(ApiSocket.class);
            if (apiSocket != null) {
                registry.addHandler(it, apiSocket.value()).setAllowedOrigins("*");
            }
        });
    }

//    @Bean
//    public ServletServerContainerFactoryBean createWebSocketContainer() {
//        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
//        //session超时时间25s，客户端25s没有交互就断开连接
//        container.setMaxSessionIdleTimeout(24 * 3600 * 1000L);
//        container.setMaxTextMessageBufferSize(10 * 1024);
//        container.setMaxBinaryMessageBufferSize(10 * 1024);
//        return container;
//    }
}
